###########################################################################
# Lipps & Sczepanski & Malet
# Understanding Preferences over Borders
# International Studies Quaterly

# Main: Analysis of Likert-scale question ----------------------------------
###########################################################################

rm(list = ls())

packages <- c("tidyverse", "ggplot2", "viridis", "fastDummies", "texreg", "ggeffects")

for (package in packages) {
  if (!requireNamespace(package, quietly = TRUE)) {
    install.packages(package, dependencies = TRUE)
  }
}

library(tidyverse)
library(ggplot2)
library(viridis)
library(fastDummies)
library(texreg)
library(ggeffects)

dat <- read.csv("survey_dat_clean.csv", na.strings=c("","NA"))
#dat_long <- read.csv("survey_dat_clean_conjoint.csv", na.strings=c("","NA"))

#recode some demographics
table(dat$sex)
dat$gender <- ifelse(dat$sex=="Männlich", 0, 1)
dat$age <- as.numeric(as.character(dat$age))

#recode vote intention
dat <- dat %>% mutate(vote_intention2 = case_when(dat$vote_intention=="Other" ~ "No answer",
                                dat$vote_intention=="No show" ~ "No answer",
                                dat$vote_intention=="Don't know" ~ "No answer", 
                                TRUE ~ dat$vote_intention))
dat$vote_intention2 <- ordered(dat$vote_intention2, levels = c("No answer", "The Left", "SPD", "Greens", "FDP", "CDU/CSU", "AfD"))

#create dummies for vote intention
pty <- fastDummies::dummy_cols(dat$vote_intention2)
dat <- cbind(dat, pty)

dat <- rename(dat,
              AfD=".data_AfD",
              CDU=".data_CDU/CSU",
              FDP=".data_FDP",
              Greens=".data_Greens",                
              SPD=".data_SPD",                 
              Left=".data_The Left") #the option "no answer" is dropped due to singularities

### Linear regression model: DV=Open-close Likert scale ------------------------------------------
mod1 <- lm(border_open_close_1 ~ AfD+FDP+CDU+SPD+Left+Greens+
             #left_right_1 + immigration_general_1 + european_integration_1 +
             gender + age + urban_rural + educat + migration_background + log(distance_closest_border),
           data=dat)
mod2 <- lm(border_open_close_1 ~ #AfD+FDP+CDU+SPD+Left+Greens+
             left_right_1 + immigration_general_1 + european_integration_1 +
             gender + age + urban_rural + educat + migration_background + log(distance_closest_border),
           data=dat)

texreg(list(mod1,mod2),
       file = "tab_1.tex",
       include.ci = F,
       digits = 3,
       single.row = TRUE,
       custom.header = list("Preference for closed border (0-10)" = 1:2))

#Figure 2: Predicted values for open/closed Likert scale------------------------------------------
mod <- lm(border_open_close_1 ~ vote_intention2 +
             #left_right_1 + immigration_general_1 + european_integration_1 +
             gender + age + urban_rural + educat + migration_background + distance_closest_border,
           data=dat)

pred <- ggpredict(mod, terms="vote_intention2")
pred$x <- ordered(pred$x, levels = c("The Left", "SPD", "Greens", "FDP", "CDU/CSU", "AfD", "No answer"))

pdf(file = "fig_2.pdf",width = 8,height = 5.5,onefile=F)

ggplot(pred, aes(x=x, y=predicted)) +
  geom_point(size=2) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), linewidth=1.25) +
  labs(x="", y="Predicted support for closed borders (0-10)") +
  theme_bw() +
  theme(axis.text.x = element_text(angle= 60, vjust=0.6, hjust=0.6, size=14, color="black"),
        axis.text.y = element_text(size=14),
        axis.title.y = element_text(size=16))

dev.off()
